﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using CsvHelper;
using UGD.Models;

namespace UGD
{
    public class csvReader
    {
        private string _filename;
        private const string ColumnSeparator = ",";
        private List<Item> _items = new List<Item>();

        public csvReader(string filename)
        {
            _filename = filename;
        }
        public bool IsFileInLocation => System.IO.File.Exists(_filename);

        public IEnumerable<Item> Read()
        {   
            using (var sr = new StreamReader(_filename))
            {
                var reader = new CsvReader(sr);


                while (reader.Read())
                {

                    string Reg = reader.GetField<string>("Name");
                    if (string.IsNullOrEmpty(Reg))
                    {
                        break;
                    }


                    if (_items.Exists(o => o.Name == Reg))
                    {
                        var record = _items.FirstOrDefault(o => o.Name == Reg);
                        var duty = new Duty();
                        duty.Start = reader.GetField<DateTime>("Start");
                        duty.End = reader.GetField<DateTime>("End");
                        record.AddDuty(duty);

                    }
                    else
                    {
                        var Item = new Item();
                        Item.Name = reader.GetField<string>("Name");
                        var duty = new Duty();
                        duty.Start = reader.GetField<DateTime>("Start");
                        duty.End = reader.GetField<DateTime>("End");
                        Item.AddDuty(duty);
                        _items.Add(Item);
                    }
                }




                return _items;
            }
        }


    }
}

